package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppFragmentHomeWorksBinding
import com.cgbl.cg.ui.WebActivity
import com.cgbl.cg.vm.home.HomeWorksViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.model.BannerBean
import com.robot.base.model.RibbonBean
import com.robot.base.model.TypeListBean
import com.robot.base.model.WorksBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeWorksFragment : GLBaseFragment<AppFragmentHomeWorksBinding, HomeWorksViewModel>() {
    private lateinit var bannerTitleAdapter: BannerImageAdapter<BannerBean>

    private lateinit var homeWorksAdapter: MoreWorksAdapter
    private lateinit var newArrivalAdapter: NewArrivalAdapter
    private lateinit var homeTabAdapter: HomeTabWorksAdapter
    private lateinit var worksRecommendAdapter: WorksRecommendAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home_works

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        viewModel.refresh()
        initBanner()
        initAdapter()
        initListener()
//        viewModel.initData()
    }

    private fun initBanner() {
        bannerTitleAdapter = object : BannerImageAdapter<BannerBean>(
            arrayListOf()
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: BannerBean?,
                position: Int,
                size: Int
            ) {
                    ImageLoaderUtils.getGlideManager()
                        .load(data?.imagesFile)
                        .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                        .into(holder?.imageView!!)
            }
        }
        binding.banner.setAdapter(bannerTitleAdapter)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setBannerGalleryEffect(12, 4)
        binding.banner.start()

        bannerTitleAdapter.setOnBannerListener { data, position ->
            WebActivity.start(requireContext(), data.hrefUrl ?: "", data?.coverName ?: "")
        }
    }

    private fun initAdapter() {
        worksRecommendAdapter = WorksRecommendAdapter()
        binding.recyclerViewRecommend.apply {
            adapter = worksRecommendAdapter
        }

        homeTabAdapter = HomeTabWorksAdapter()
        binding.recyclerViewTab.apply {
            adapter = homeTabAdapter
        }
        homeTabAdapter.setOnItemClickListener { adapter, view, position ->
            newArrivalAdapter.setNewInstance(homeTabAdapter.data[position].typeWorksByDateList as MutableList<WorksBean>?)
        }
        newArrivalAdapter = NewArrivalAdapter()
        binding.recyclerViewNewArrival.apply {
            adapter = newArrivalAdapter
        }

        homeWorksAdapter = MoreWorksAdapter()
        binding.recyclerViewMoreWorks.apply {
            adapter = homeWorksAdapter
        }
    }

    private fun initListener() {
//        binding.smartRefreshLayout.setOnLoadMoreListener {
//            it.finishLoadMore()
//            viewModel.toDiscoveryCollectionCommand.execute()
//        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.ribbonDataEvent.observe(this) {
            bannerTitleAdapter.setDatas(it)
        }

        viewModel.uc.refreshEvent.observe(this) {
            it ?: return@observe
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
        }
        viewModel.uc.tabData.observe(this) {
            homeTabAdapter.setNewInstance(it as MutableList<TypeListBean>)

            if (it.isEmpty()) return@observe
            newArrivalAdapter.setNewInstance(it[0].typeWorksByDateList as MutableList<WorksBean>?)
        }
        viewModel.uc.hotData.observe(this) {
            worksRecommendAdapter.setNewInstance(it as MutableList<WorksBean>?)
        }

        viewModel.uc.moreData.observe(this) {
            homeWorksAdapter.setNewInstance(it as MutableList<WorksBean>?)
        }
    }

}